<?php
class Model_CupKoTeam extends Zend_Db_Table_Abstract
{
	protected $_name    = 'g1_cup_ko_team';
	protected $_primary = 'id';

    public function addTeam($cup_id,$team_id,$team_name,$user_name){
    	
      $data['cup_ko_id']=$cup_id;
      $data['team_id']=$team_id;     
      $data['team_name']=$team_name;
      if(empty($user_name)){
      	$data['user_id'] = null;
      }else{
      	$model_user = new Model_User();
      	$data['user_id']=$model_user->getUserByName($user_name)->id;
      }

     

      $select = $this->select()
      			->from($this, 'count(id) as count')
      			->where('cup_ko_id = ?', $cup_id)
      			->where('team_id = ?',$team_id)	;
      	
      $count = $this->fetchRow($select);
      
      $dispo = ($count->count == 0);
      if($dispo){
      	$this->insert($data);
      }else{
      	//$this->update($data, 'league_id='.$league_id . ' and team_id = ' . $team_id);
      }
      
    }
    
    public function modifyTeam($cup_id,$team_id,$team_name,$user_name){
    	
    	$data['team_name']=$team_name;
    
    	if(empty($user_name)){
    		$data['user_id'] = null;
    	}else{
    		$model_user = new Model_User();
    		$data['user_id']=$model_user->getUserByName($user_name)->id;
    		
    	}
    	
    
    
    	
    	
    	if(!empty($user_name)){
	    	
	    	   $model_cup_game = new Model_CupGame();
	    	   $model_cup_game->addGamesByCupIdAndTeamId($cup_id,$team_id);
	    
    	
    	}
    	
    	
    		$this->update($data, 'cup_ko_id='.$cup_id . ' and team_id = ' . $team_id);
    	
    	
    	
    }
    
 
    
    public function getTeamsByCupKoId($cup_ko_id){
    	$select = " select u.username, lt.*
    			    from g1_cup_ko_team lt left join g1_user u on lt.user_id = u.id
    			    where lt.cup_ko_id = " . $cup_ko_id . " order by lt.team_id asc";
			
		return $this->_db->fetchAll( $select );
    }
    public function getActiveTeamsByCupId($cup_id){
    	$select = " select u.username, lt.*
    			    from g1_cup_team lt join g1_user u on lt.user_id = u.id
    			    where lt.team_name is not null and lt.cup_id = " . $cup_id . " order by lt.team_id asc";
    		
    	return $this->_db->fetchAll( $select );
    }
    
    public function getFirstCupIdByUserId($user_id){
    	$select = $this->select('cup_ko_id')
    			->where('user_id=?', $user_id);
    	
    	$row = $this->fetchRow($select);
    	return $row;
    }
    
    public function getUsersByCupId($cup_id){
    	$select = " select u.id, u.username
    			    from g1_cup_team lt, g1_user u
    			    where 
    			          lt.user_id = u.id and lt.cup_id = " . $cup_id;
			
		return $this->_db->fetchAll( $select );
    }
    
    public function getCupsByUserId($user_id){
    	$select = " select lt.cup_ko_id, l.name
    			    from g1_cup_ko_team lt, g1_cup_ko l
    			    where lt.cup_ko_id = l.id and lt.user_id = " . $user_id;
			
		return $this->_db->fetchAll( $select );
    }
	
	public function getCupTeamInfoByCupIdAndTeamId($cup_id,$team_id){
	    	$select = $this->select()
			->where('cup_ko_id=?', $cup_id)
    			->where('team_id=?', $team_id);
    	
    	$row = $this->fetchRow($select);
    	return $row;
	}
	
	public function getCupTeamInfoByCupIdAndGroupNameAndTeamId($cup_id,$group_name,$team_id){
		$select = $this->select()
			->where('cup_id=?', $cup_id)
			->where('group_name=?', $group_name);
		
		$rows = $this->fetchAll($select);
	
		return $rows[$team_id -1];
	}
	
	public function deleteUserByCupIdAndTeamId($cup_id,$team_id){

		$data['user_id'] = null;
		$this->update($data, 'cup_id='.$cup_id . ' and team_id = ' . $team_id);
		
		$model_cup_game = new Model_CupGame();
		
		$model_cup_game->deleteGamesByCupIdAndTeamId($cup_id, $team_id);
	}
	   
	public function getTeamsByGroup($cup_id,$group_name){
		$select = $this->select()
			->where('cup_id=?',$cup_id)
			->where('group_name=?',$group_name);
			
		$row = $this->fetchAll($select);
		return $row;
	}
	
	public function getTeamByCupIdAndTeamName($cup_id,$team_name){
		$select = $this->select()
				->where('cup_ko_id=?',$cup_id)
				->where('team_name=?',$team_name);
			
		$row = $this->fetchRow($select);
		return $row;
	}
	
	public function getGroupNameByCupIdAndUser($cup_id,$user_id){
		$select = $this->select('group_name')
		->where('cup_id=?',$cup_id)
		->where('user_id=?',$user_id);
			
		$row = $this->fetchRow($select);
		return $row;
	}
	
	public function getCups(){
		$select = " select lt.cup_id, l.name
    			    from g1_cup_team lt, g1_cup l
    			    where lt.cup_id = l.id group by lt.cup_id";
	
		return $this->_db->fetchAll( $select );
	}
	
	public function getGroupsByCupId($cup_id){
		$select = " select group_name
    			    from g1_cup_team  
				    where cup_id ='" . $cup_id . "'
				     group by group_name ";
		
		return $this->_db->fetchAll( $select );
	}
	
	public function getTeamCountByCupIdAndGroupName($cup_id,$group_name){
		$select = " select count(id) as num
    			    from g1_cup_team
				    where cup_id ='" . $cup_id . "' 
				         and group_name='".$group_name . "'";

		return $this->_db->fetchRow( $select );
	}
	
	 public function getFirstActiveCupIdByUserId($user_id){
    	$select = " select ct.cup_ko_id
    			    from g1_cup_ko_team ct, g1_cup_ko c
    			    where status='new' and ct.cup_ko_id = c.id and ct.user_id = " . $user_id;
        $row = $this->_db->fetchRow($select);
    	return $row;
    }
	
		public function getActiveCupsByUserId($user_id){
		$select = " select lt.cup_ko_id, l.name
    			    from g1_cup_ko_team lt, g1_cup_ko l
    			    where status='new' and lt.cup_ko_id = l.id and lt.user_id = " . $user_id;
			
		return $this->_db->fetchAll( $select );
	}
}